package com.lzh.educms.service.impl;

import com.lzh.commonutils.CommonResult;
import com.lzh.educms.client.OssClient;
import com.lzh.educms.entity.CrmBanner;
import com.lzh.educms.mapper.CrmBannerMapper;
import com.lzh.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author lizuhu
 * @since 2021-05-16
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

   @Autowired
   private OssClient ossClient;
    /**
     * 查询所有轮播图
     * @return
     */
    @Cacheable(key = "'selectList'",value = "banner")
    @Override
    public List<CrmBanner> selectAllBanner() {
        List<CrmBanner> list = baseMapper.selectList(null);
        return list;
    }

    /**
     * 删除轮播图
     * @param id
     * @return
     */
    @Override
    public boolean removeBannerById(String id) {
        CrmBanner banner = baseMapper.selectById(id);
        String imageUrl = banner.getImageUrl();
        if (!StringUtils.isEmpty(imageUrl)) {
            String path = imageUrl.substring(imageUrl.lastIndexOf("/")+1);
            CommonResult result = ossClient.deleteAvatar(path);
            if (result.getCode() == 200) {
                System.out.println("图片删除成功");
            }
        }
        Integer result = baseMapper.deleteById(id);
        return result != null && result > 0;
    }
}
